<?php
//error_reporting(E_ALL);
//require_once('config.php');

function create_vhost($domain,$ip)
{
	include('config.php');
	echo "<p><b>CREATING CONFIGURATION FOR DOMAIN $domain</p></b>";
	echo '<p>Checking ip address: ';
	$ip_isset=false;																					//	IP Available test Section
	$local_ip_list=shell_exec("$ip_bin a | grep 'inet ' | awk '{ print $2 }' | cut -f1 -d'/' ");
	if ($ip == '*') $ip_isset=true;
	foreach(preg_split("/(\r?\n)/", $local_ip_list) as $line)
	{
		if ($ip == $line) $ip_isset=true;
	}
	if (!$ip_isset) 																					//	Break if ip address unavailable
	{
		echo "FALSE</p>";
		return 'Unavailable IP address';
		break;
	}
	echo "OK</p>";
																										//	Vhost Available Test
	echo "<p>Checking current VirtualHost's: ";
	$vhost_free=false;
	$grep_domain=shell_exec("$apache2ctl_bin -S 2>/dev/null | grep $domain");
	echo $grep_domain;
	if ($grep_domain != '')
	{
		echo "FALSE</p>";
		return "VirtualHost already exists:\n$grep_domain";
		break;																							// Break if VirtualHost already exists
	}
	echo "OK</p>";
																										//	All checks passed, creating VHost
	echo "<p>Creating DocumentsRoot directory: ";
	$docroot=$htdocs_root.$domain;
	echo shell_exec("mkdir $htdocs_root$domain 2>&1");
	echo " OK</p>";
	echo "<p>Building VHost config file: ";																//	Building VHost config file
	$default_vhost = preg_replace('/\[domain\]/',$domain,$default_vhost);
	$default_vhost = preg_replace('/\[ip\]/',$ip,$default_vhost);
	$default_vhost = preg_replace('/\[docroot\]/',$docroot,$default_vhost);
	//echo $default_vhost;
	echo shell_exec("echo '$default_vhost' > $apache_vhost_config_dir$domain 2>&1");
	//echo shell_exec("sudo $apache_init restart 2>&1");
	echo "OK</p>";
		
	return true;																						//	All done function returns true
}

function create_ftp($domain,$username,$password)
{
	include('config.php');
	$domain=addslashes($domain);
	$username=addslashes($username);
	//$password=addslashes($password);
	echo "<p>Creating FTP user $username echo $password: ";
	$result = shell_exec("sudo $pure_pw_bin list | grep $username 2>&1");								//	Getting users list
	if ($result != "")																					//	If user exists - reconfiguring it
	{
		echo "User exists! reconfiguring user $username: ";
		shell_exec("sudo $pure_pw_bin usermod $username -u $pure_pw_uid -g $pure_pw_gid -d $htdocs_root$domain");
		shell_exec("( echo \"$password\"; echo \"$password\" ) | sudo $pure_pw_bin passwd $username");
		shell_exec("sudo $pure_pw_bin mkdb");
		echo "OK</p>";
	} else																								// If new user - creating it
	{
		shell_exec("( echo \"$password\"; echo \"$password\" ) | sudo $pure_pw_bin useradd $username -u $pure_pw_uid -g $pure_pw_gid -d $htdocs_root$domain");
		shell_exec("sudo $pure_pw_bin mkdb");
		echo "OK</p>";
	}
	
return true;	
}

function create_db($username,$password,$database)
{
	$username=addslashes($username);
	$password=addslashes($password);
	$database=addslashes($database);
	include ('config.php');
	echo "<p>Creating MySQL Database '$database' and User '$username' Pass '$password':";
	$mysql_connect=mysql_connect($mysql_host,$mysql_user,$mysql_pass);
	if (!$mysql_connect)
	{
		return mysql_error();
		break;
	}
	
	$query="SELECT user from mysql.user WHERE user='$username'";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) if ($row['user'] != '')									//	Checking if User already exists if yes - break
	{
		return "MySQL user $username already exists, break\n";
		break;
	}
	
	$query="select db from mysql.db where db='$database';";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) if ($row['db'] != '')										//	Checking if Database already exists if yes - break
	{
		return "MySQL DataBase '$database' already exists, break\n";
		break;
	}
	
	mysql_query("CREATE database $database");															//	Creating Database
	mysql_query("GRANT ALL PRIVILEGES ON $database.* TO $username@'localhost' IDENTIFIED BY '$password';") or die (mysql_error());	//	Creating User
	mysql_query("FLUSH PRIVILEGES;");
	echo "OK</p>";
	
	
	mysql_close($mysql_connect);
	return true;
}
require_once('config.php');

//echo create_vhost("sath.pp.uaa","127.0.0.1",false);
//echo create_ftp('sath.pp.uaa','sath','max');
//echo create_db('sath','sath_password','sath_db');

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Web Hosting Script</title>

<form action="?go" method="post">
Domain:<input type="text" name="domain" />
IP:<select name="ip">
<option selected value="*">*(all)</option>
<?
$local_ip_list=shell_exec("$ip_bin a | grep 'inet ' | awk '{ print $2 }' | cut -f1 -d'/' ");
foreach(preg_split("/(\r?\n)/", $local_ip_list) as $line) echo "<option value=\"$line\">$line</option>\n";
?>
</select>
FTP Username:<input type="text" name="ftp_user" />
FTP Password:<input type="text" name="ftp_pass" />
DB Name:<input type="text" name="db_name" />
DB User:<input type="text" name="db_user" />
DB Pass:<input type="text" name="db_pass" />
<input type="submit" name="Submit" />
</form>
<?
if (isset ($_GET['go'])&&$_POST['domain']!=''&&$_POST['ip']!=''&&$_POST['ftp_user']!=''&&$_POST['ftp_pass']!=''&&$_POST['db_name']!=''&&$_POST['db_user']!=''&&$_POST['db_pass']!='')
{
		$domain=$_POST['domain'];
		echo '<p>';
		$return = create_vhost($_POST['domain'],$_POST['ip']);
		if ($return == 1)
		$return = create_ftp($_POST['domain'],$_POST['ftp_user'],$_POST['ftp_pass']);
		if ($return == 1)
		$return = create_db($_POST['db_user'],$_POST['db_pass'],$_POST['db_name']);
		if ($return != 1) echo '<p style="color:#FF0000"><b>'.$return.'</b></p>'; else
		echo "<p style=\"color:#00FF00\"><b>Configuration for $domain successfully created</b></p>";
		echo shell_exec("sudo $apache_init restart 2>&1");
		echo '</p>';
}
?>
</head>
</html>
